package com.xxxx.yeb.controller;


import com.xxxx.yeb.pojo.*;
import com.xxxx.yeb.service.impl.*;
import com.xxxx.yeb.utils.PoiUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-07-17
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;


    @Autowired
    private PositionServiceImpl positionService;

    @Autowired
    private PoliticsStatusServiceImpl politicsStatusService;
    @Autowired
    private NationServiceImpl nationService;
    @Autowired
    private JoblevelServiceImpl joblevelService;
    @Autowired
    private DepartmentServiceImpl departmentService;


    /**
     * 多条件查询所有员工信息
     *
     * @param employeeQuery
     * @return
     */
    @ApiOperation(value = "查询所有员工")
    @GetMapping("/")
    public EmployeeQuery selectAllEmployeeByQuery(EmployeeQuery employeeQuery) {
        return employeeService.selectAllEmployeeByQuery(employeeQuery);
    }

    @ApiOperation(value = "查询所有职位")
    @GetMapping("/positions")
    public List<Position> selectAllPositions() {
        return positionService.queryAllPosition();
    }

    @ApiOperation(value = "查询所有民族")
    @GetMapping("/nations")
    public List<Nation> selectAllNationStatus() {
        return nationService.selectAllNation();
    }

    @ApiOperation(value = "查询所有职级")
    @GetMapping("/joblevels")
    public List<Joblevel> selectAllJobLevel() {
        return joblevelService.selectAllJobLevel();
    }

    @ApiOperation(value = "查询所有部门")
    @GetMapping("/deps")
    public List<Department> selectAllDepartment() {
        return departmentService.selectAllDepartment();
    }

    @ApiOperation(value = "查询所有政治面貌")
    @GetMapping("/politicsstatus")
    public List<PoliticsStatus> selectAllPoliticsStatus() {
        return politicsStatusService.selectAllPoliticsStatus();
    }

    @ApiOperation(value = "查询最大员工号")
    @GetMapping("/maxWorkID")
    public EmployeeQuery queryMaxWorkID() {
        return employeeService.queryMaxWorkID();
    }

    /**
     * 编辑员信息通用接口
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "编辑员工信息")
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateEmployee(@RequestBody Employee employee) {
        return employeeService.updateEmployeeByEmp(employee);
    }

    /**
     * 添加员信息通用接口
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "添加员工信息")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public RespBean insertEmployee(@RequestBody Employee employee) {

        return employeeService.insertEmployeeByEmp(employee);
    }

    /**
     * 删除员信息接口
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "通过id删除员工信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RespBean deleteEmployeeById(@PathVariable Integer id) {
        return employeeService.deleteEmployeeById(id);
    }

    /**
     * 导出所有员工信息
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void excelExport(HttpServletResponse response) throws IOException {
        employeeService.excelExport(response);
    }

    /**
     * 导入员工信息
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/import")
    public RespBean importEmployee(@RequestParam("file") MultipartFile file) throws IOException {
        //获取民族
        List<Nation> allNations = nationService.selectAllNation();
        System.out.println(allNations.size());
        //获取政治面貌
        List<PoliticsStatus> allPolitics = politicsStatusService.selectAllPoliticsStatus();
        System.out.println(allPolitics.size());
        //获取部门
        List<Department> allDeps = departmentService.selectAllDepartment();
        System.out.println(allDeps.size());
        //获取职称
        List<Position> allPos = positionService.queryAllPosition();
        //获取职位
        List<Joblevel> allJobLevels = joblevelService.selectAllJobLevel();
        //解析上传的excel，并获取到里面单元格的值
        List<Employee> emps = PoiUtils.importEmployeeByExcel(file, allNations, allPolitics, allDeps, allPos, allJobLevels);
        //判断新添加的员工数量与从excel中获取的员工数量一致，则代表导入成功
        if (employeeService.saveBatch(emps)) {
            return RespBean.success("导入成功");
        } else {
            return RespBean.error("导入失败");
        }
    }
}

